package com.gnerv.sylvanas.framework.plugin.datasource.listener;

import com.gnerv.sylvanas.framework.plugin.datasource.service.DataSourceService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.context.ApplicationListener;

import java.util.Set;

@Slf4j
public class DatasourceEnvironmentChangeEventListener implements ApplicationListener<EnvironmentChangeEvent> {

    @Resource
    private DataSourceService dataSourceService;

    @Override
    public void onApplicationEvent(EnvironmentChangeEvent environmentChangeEvent) {
        Set<String> keys = environmentChangeEvent.getKeys();
        boolean has = keys.stream().anyMatch(key -> key.startsWith("spring.datasource.dynamic"));
        if (has){
            dataSourceService.reload();
        }
    }
}
